package com.future.controller;

import com.future.allUtils.constants.PermissionConstants;
import com.future.domain.DTO.SearchDTO;
import com.future.domain.DTO.SearchResultDTO;
import com.future.domain.Result;
import com.future.service.SearchService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SearchController {
    @Autowired
    private SearchService searchService;

   /**
    * 前台搜索
    * @param searchDTO(搜索关键词(必有),页码,每页数量)
    * @return 搜索结果
    */
    @PostMapping("/search")
   public Result receptionSearch(@RequestBody SearchDTO searchDTO) {
       // 关键词非空校验
       if (searchDTO.getKeyword() == null || searchDTO.getKeyword().trim().isEmpty()) {
           return Result.fail("搜索关键词不能为空");
       }
       return Result.ok(searchService.receptionSearch(searchDTO));
   }
   /**
    * 后台搜索
    * @param searchDTO(搜索关键词(必有),页码,每页数量,搜索类型)
    * @return 搜索结果
    */
   @PostMapping("/admin/search")
   public Result backgroundSearch(@RequestBody SearchDTO searchDTO) {
       // 关键词非空校验
       if (searchDTO.getKeyword() == null || searchDTO.getKeyword().trim().isEmpty()) {
           return Result.fail("搜索关键词不能为空");
       }
       return Result.ok(searchService.backgroundSearch(searchDTO));
   }


   @PreAuthorize("hasAuthority('" + PermissionConstants.TEACHER_STUDENT_MANAGE + "')")
   @ApiOperation("教师查询本班学生的信息")
   @PostMapping("/teacher/search")
    public Result teacherSearch(@RequestBody SearchDTO searchDTO) {
       // 关键词非空校验
       if (searchDTO.getKeyword() == null || searchDTO.getKeyword().trim().isEmpty()) {
           return Result.fail("搜索关键词不能为空");
       }
       SearchResultDTO searchResultDTO = searchService.teacherSearch(searchDTO);
       return Result.ok(searchResultDTO);
   }


}
